package org.gwtEss.portlet;

import org.gwtEss.other.Dragger;

import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.client.ui.HTMLTable.CellFormatter;

public abstract class Portlet extends PopupPanel{
	static int maxIndex = 1;//Provide a better implementation here ... A good idea would be to have an getInstance class and maintain all the instances and zindexes in one place for the popups. 
//	VerticalPanel vPanel = new VerticalPanel();
	Grid mainPanel = new Grid(3,3);
	Dragger title = new Dragger(){
		int vPanelTop, vPanelLeft; 
		@Override
		public void onDrag(int mouseX, int mouseY, int deltaX, int deltaY) {
			rePosition(deltaX, deltaY);
			Portlet.this.onDrag(mouseX, mouseY);
		}
		private void rePosition(int deltaX, int deltaY){
			int newLeft = vPanelLeft - deltaX;
			newLeft = newLeft < 0?0:newLeft;
			int newTop = vPanelTop - deltaY;
			newTop = newTop < 0?0: newTop;
			
			Portlet.this.setPopupPosition(newLeft, newTop);
//			DOM.setStyleAttribute(PortletNew1.this.getElement(), "left", newLeft + "px");
//			DOM.setStyleAttribute(PortletNew1.this.getElement(), "top", newTop + "px");
		}
		
		@Override
		public void onDragEnd(int mouseX, int mouseY, int deltaX, int deltaY) {
			rePosition(deltaX, deltaY);
			Portlet.this.onDragEnd(mouseX, mouseY);
			DOM.releaseCapture(getElement());
			mainPanel.removeStyleName("transbox");
//			Portlet.this.getElement().getStyle().setProperty("zIndex", "0");
		}

		@Override
		public void onDragStart(int mouseX, int clientY) {
//			DialogBox db = new DialogBox(); db.show();
//			hide();
//			show();
//			rePosition(0, 0);
//		    DOM.setCapture(getElement());
//		    super.addStyleName("zi");
			vPanelTop = Portlet.this.getAbsoluteTop(); 
			vPanelLeft = Portlet.this.getAbsoluteLeft(); 
			Portlet.this.onDragStart(mouseX, clientY);
			Portlet.this.getElement().getStyle().setProperty("zIndex", maxIndex++ + ""/*"1"*/);
			
			mainPanel.addStyleName("transbox");
		}};
		ScrollPanel bodyPlaceHolder = new ScrollPanel();

		@Override
		public int getOffsetHeight() {
			return mainPanel.getOffsetHeight();
		}
	@Override
	public void setWidth(String width) {
//		bodyPlaceHolder.setWidth(width);
		mainPanel.setWidth(width);
//		super.setWidth(width);
	}
	
	@Override
	public void setHeight(String height) {
		mainPanel.setHeight(height);
//		super.setHeight(height);
	}
	
	@Override
	public void setSize(String width, String height) {
		setWidth(width);
		setHeight(height);
//		super.setSize(width, height);
	}
	public Portlet() {
		super(false, false);
		super.setAnimationEnabled(true);
		title.setWidget(new HTML("<i>title not set</i>"));
		title.addStyleName("table-header");
		DOM.setStyleAttribute(getElement(), "padding", "0px");
		DOM.setStyleAttribute(getElement(), "border", "0px solid #C3D9FF");
		
		Portlet.this.getElement().getStyle().setProperty("zIndex", "0");

		mainPanel = getBorder(new VerticalPanel(){
			{
				add(title);
				add(bodyPlaceHolder);
			}
		});
		mainPanel.getElement().getStyle().setProperty("position", "absolute");

//		vPanel.add(title);
//		vPanel.add(bodyPlaceHolder);
		
		
//		vPanel.addStyleName("portlet");
//		vPanel.addStyleName("s");
		
		
//		super.setWidget(getBorder(vPanel));
		super.setWidget(mainPanel);
	}
	
	private Grid getBorder(Widget w){
		Grid b = new Grid(3,3);
		CellFormatter cellFormatter = b.getCellFormatter();
		for (int i=0; i< 3; i++)
			for (int j=0; j< 3; j++){
				b.setText(i, j, "");
				cellFormatter.addStyleName(i, j, "border" + i + "" + j);
			}
		b.setWidget(1, 1, w);
		b.setBorderWidth(0);
		b.setCellPadding(0);
		b.setCellSpacing(0);
		return b; 
	}
	
	@Override
	protected void onAttach() {
		super.onAttach();
		final int height = bodyPlaceHolder.getOffsetHeight();

		if (height > 200)
			bodyPlaceHolder.setHeight("100px");
	
	}
/*	@Override
	public void addStyleName(String style) {
		mainPanel.addStyleName(style);
//		super.addStyleName(style);
	}
	@Override
	public void removeStyleName(String style) {
		mainPanel.removeStyleName(style);
//		super.removeStyleName(style);
	}*/
	public void setBodyWidget(Widget widget) {
		bodyPlaceHolder.setWidget(widget);
	
	}
	public void setTitleWidget(Widget titleWidget) {
		title.setWidget(titleWidget);
	}
	
	@Override
	public void setWidget(Widget widget) {
		assert false: "Use setBodyWidget(w) instead";
	}
	public abstract void onDrag(int mouseX, int mouseY);
	public abstract void onDragEnd(int mouseX, int mouseY);
	public abstract void onDragStart(int mouseX, int mouseY);
}
